package DailyPractice.Day6;

/**
 * 将字符串转为整数
 * @author lumin
 * @date 2022/04/23 10:02
 **/
public class Num2 {
    public static int StrToInt(String str) {
        int num = 0;
        int midNum = 1;
        int i = str.length() - 1;
        while(i >= 0){
            if(i == 0 && str.charAt(i) == '-'){
                return -num;
            }
            if(Snum(str,i) == -1){
                return 0;
            }
            num += midNum * Snum(str,i);
            midNum = midNum * 10;
            i--;
        }
        return num;
    }

    private static int Snum(String str, int i) {
        if(str.charAt(i) == '0'){
            return 0;
        }else if(str.charAt(i) == '1'){
            return 1;
        }else if(str.charAt(i) == '2'){
            return 2;
        }else if(str.charAt(i) == '3'){
            return 3;
        }else if(str.charAt(i) == '4'){
            return 4;
        }else if(str.charAt(i) == '5'){
            return 5;
        }else if(str.charAt(i) == '6'){
            return 6;
        }else if(str.charAt(i) == '7'){
            return 7;
        }else if(str.charAt(i) == '8'){
            return 8;
        }else if(str.charAt(i) == '9'){
            return 9;
        }else if(str.charAt(i) == '+'){
            return 0;
        }else{
            return -1;
        }
    }

    public static void main(String[] args) {
        String str = "-13";
        System.out.println(StrToInt(str));
    }
}